import { app } from 'electron';

// 设置安全策略
export function setupSecurity() {
  // 设置内容安全策略
  app.on('web-contents-created', (event, contents) => {
    // 禁止导航到外部URL
    contents.on('will-navigate', (event, navigationUrl) => {
      const parsedUrl = new URL(navigationUrl);
      
      // 只允许导航到本地文件或开发服务器
      if (parsedUrl.origin !== 'http://localhost:5173' && !navigationUrl.startsWith('file://')) {
        event.preventDefault();
      }
    });

    // 禁止打开新窗口
    contents.on('new-window', (event, navigationUrl) => {
      event.preventDefault();
    });

    // 在生产环境中设置CSP
    if (!app.isPackaged) {
      contents.session.webRequest.onHeadersReceived((details, callback) => {
        callback({
          responseHeaders: {
            ...details.responseHeaders,
            'Content-Security-Policy': ['default-src \'self\' \'unsafe-inline\' \'unsafe-eval\' data: blob:']
          }
        });
      });
    }
  });
}